#!/usr/local/bin/python
INCLUDE_PATH = 'C:\\Documents and Settings\\Clint\\My Documents\\pythonlib'

# distutils
def setup():
    from cx_Freeze import setup, Executable
    import sys
    setup(name = "setMayanWallpaper",
          version = "0.1",
          description = "Mayan Windows Desktop Wallpaper rendering.",
          executables = [Executable("mayan_cal/script.py",
                                    path = sys.path + [INCLUDE_PATH])],
          packages = ['mayan_cal'])

MODULES = ('mayan_cal', 'mayan_cal.structure',   'mayan_cal.calculate',
                        'mayan_cal.affirmation', 'mayan_cal.drawing',
                        'mayan_cal.desktop')

def doublequote_repr(o):
    return '"' + repr("'" + str(o))[2:]

def main():
    # Todo: Don't use --include-modules, use --package
    target_dir = "Mayan Calendar"
    target_name = "MayanWallpaper.exe"

    prolog = "cmd /C \"python C:\\cygwin\\bin\\freeze --include-path=. --include-modules="
    modules = ','.join(MODULES)
    epilog = " mayan_cal\\script.py --target-dir=%s --target-name=%s\"" % \
             (doublequote_repr(target_dir), doublequote_repr(target_name))

    from os import system as system_command
    from os.path import join as joinpath

    verbose = True
    def shell_command(cmd):
        if verbose:
            print cmd

        return system_command(cmd)

    mkdir  = lambda path: shell_command("mkdir -p " + doublequote_repr(path))
    copy   = lambda path, *files: shell_command("cp -Rf %s %s" % \
                                    (" ".join(doublequote_repr(f) for f in files),
                                     doublequote_repr(path)))

    export = lambda tree, path: shell_command(("svn export " + \
                                               "file:///repository/trunk/MayanCalendar/%s" + \
                                               " %s") % (tree, doublequote_repr(path)))

    # Run cx freeze.
    shell_command(prolog + modules + epilog)

    # Copy glyph data files.
    export("glyphs", joinpath(target_dir, "glyphs"))

    # Make a temporary directory tree.
    mkdir(joinpath(target_dir, "affirmations", "daily"))

    # Copy preferences ini and shortcut.
    copy(target_dir, "Preferences.reg", "Wallpaper Update.lnk")

    # Generate post-install script for putting data files and output folder
    # into Program Files, and updating the registry with the config file.

    # Generate short-cuts.
    # Generate schedule-task.
    # Generate registry-config.
    # Platform.createShortcut(joinpath("dist", "Mayan Wallpaper Update"),
    #                         target_path = 'mayanCal.exe', run_in = '',
    #                         '--verbose')

if __name__ == '__main__':
    main()
